package com.module.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.module.framework.domain.Book;
import com.module.framework.domain.Indemnify;
import com.module.framework.domain.Public;
import com.module.framework.domain.User;
import com.module.framework.dto.IndemnifyDTO;
import com.module.framework.dto.IndemnifyListDTO;
import com.module.framework.mapper.BookMapper;
import com.module.framework.mapper.IndemnifyMapper;
import com.module.framework.mapper.UserMapper;
import com.module.framework.service.IndemnifyService;
import com.module.framework.vo.IndemnifyVO;
import com.module.framework.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class IndemnifyServiceImpl extends ServiceImpl<IndemnifyMapper, Indemnify> implements IndemnifyService {
    @Resource
    private BookMapper bookMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private IndemnifyMapper indemnifyMapper;

    @Override
    public void add(IndemnifyDTO indemnifyDTO) {
        //1根据用户名获取用户信息
        QueryWrapper<User> userQueryWrappe = new QueryWrapper<>();
        userQueryWrappe.eq("user_name", indemnifyDTO.getUser());
        User user = userMapper.selectOne(userQueryWrappe);

        //2根据图书名获取图书信息
        QueryWrapper<Book> bookQueryWrappe = new QueryWrapper<>();
        bookQueryWrappe.eq("name", indemnifyDTO.getBook());
        Book book = bookMapper.selectOne(bookQueryWrappe);

        //3.完善Indemnify信息
        Indemnify indemnify = new Indemnify();
        indemnify.setUserId(user.getId());
        indemnify.setBookId(book.getId());
        indemnify.setCompensationAmount(indemnifyDTO.getAmount());
        indemnify.setCompensationReason(indemnifyDTO.getReason());
        indemnify.setCompensationStatus("1");

        //4.插入数据
        indemnifyMapper.insert(indemnify);


    }

    @Override
    public PageVO getPage(IndemnifyListDTO indemnifyListDTO) {
        Integer pageNum = indemnifyListDTO.getPageNum();
        LambdaQueryWrapper<Indemnify> queryWrapper = new LambdaQueryWrapper<>();

        if (Objects.nonNull(indemnifyListDTO.getUser())) {
            // 根据用户名获取用户信息
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("user_name", indemnifyListDTO.getUser());
            User user = userMapper.selectOne(userQueryWrapper);
            if (user != null) {
                // 完善Indemnify的条件
                queryWrapper.eq(Indemnify::getUserId, user.getId());
            }
        }
        if (Objects.nonNull(indemnifyListDTO.getBook())) {
            // 根据图书名获取图书信息
            QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
            bookQueryWrapper.eq("name", indemnifyListDTO.getBook());
            Book book = bookMapper.selectOne(bookQueryWrapper);
            if (book != null) {
                // 完善Indemnify的条件
                queryWrapper.eq(Indemnify::getBookId, book.getId());
            }
        }
        if (Objects.nonNull(indemnifyListDTO.getStatus())) {
            // 完善Indemnify的条件
            queryWrapper.eq(Indemnify::getCompensationStatus, indemnifyListDTO.getStatus());
        }

        Page<Indemnify> page = new Page<>(pageNum, indemnifyListDTO.getPageSize());
        IPage<Indemnify> pageResult = indemnifyMapper.selectPage(page, queryWrapper);

        // 获取用户id和书籍id对应的名字
        List<Indemnify> indemnifyList = pageResult.getRecords();
        List<IndemnifyVO> indemnifyVOList = new ArrayList<>();
        for (Indemnify indemnify : indemnifyList) {
            // 创建IndemnifyVO对象
            IndemnifyVO indemnifyVO = new IndemnifyVO();
            // 将Indemnify中的基本字段复制到IndemnifyVO
            BeanUtils.copyProperties(indemnify, indemnifyVO);  // 假设使用了BeanUtils来复制属性

            // 查询用户信息并设置到VO
            User user = userMapper.selectById(indemnify.getUserId());
            indemnifyVO.setUserName(user.getUserName());  // 设置用户名字

            // 查询图书信息并设置到VO
            Book book = bookMapper.selectById(indemnify.getBookId());
            indemnifyVO.setBookName(book.getName());  // 设置书籍名字

            // 将VO添加到VO列表
            indemnifyVOList.add(indemnifyVO);
        }

        // 返回包含所有数据的PageVO
        PageVO pageVO = new PageVO(indemnifyVOList, (int) pageResult.getTotal());
        return pageVO;
    }

}
